package com.macro.mall.controller;

import com.alibaba.fastjson.JSON;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.e.SysTalkEntDTO;
import com.macro.mall.model.BasicTopic;
import com.macro.mall.service.SysTalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/talk")
public class SysTalkController {
    @Autowired
    private SysTalkService sysTalkService;

    @GetMapping("/list")
    public CommonResult list(SysTalkEntDTO sysTalkEntDTO) {
        return sysTalkService.list(sysTalkEntDTO);
    }

    @RequestMapping("/get")
    public CommonResult get(@RequestBody SysTalkEntDTO sysTalkEntDTO) {
        return null;
    }


    /***
     * 新增话题配置
     * @param sysTalkEntDTO
     * @return
     */
    @PostMapping("add")
    public CommonResult add(@RequestBody SysTalkEntDTO sysTalkEntDTO) {
        BasicTopic topic = new BasicTopic();
        topic.setJudgmentResults(sysTalkEntDTO.getJudgmentResults());
        topic.setTopicName(sysTalkEntDTO.getTopicName());
        if (!sysTalkEntDTO.getKeyList().isEmpty()){
            List<String> keyList = sysTalkEntDTO.getKeyList();
            String jsonString = JSON.toJSONString(keyList);
            topic.setKeyList(jsonString);
        }
        return sysTalkService.edit(topic);
    }


    /***
     * 编辑话题配置
     * @param sysTalkEntDTO
     * @return
     */
    @PostMapping("/modify")
    public CommonResult modify(@RequestBody SysTalkEntDTO sysTalkEntDTO) {
        BasicTopic basicTopic = new BasicTopic();
        basicTopic.setId(sysTalkEntDTO.getId());
        basicTopic.setStatus(sysTalkEntDTO.getStatus());
        basicTopic.setTopicName(sysTalkEntDTO.getTopicName());
        basicTopic.setJudgmentResults(sysTalkEntDTO.getJudgmentResults());
        if (!sysTalkEntDTO.getKeyList().isEmpty()){
            List<String> keyList = sysTalkEntDTO.getKeyList();
            String jsonString = JSON.toJSONString(keyList);
            basicTopic.setKeyList(jsonString);
        }
        return sysTalkService.edit(basicTopic);
    }

    /***
     * 修改话题配置 状态
     * @param sysTalkEntDTO
     * @return
     */
    @PostMapping("/setStatus")
    public CommonResult setOn(@RequestBody SysTalkEntDTO sysTalkEntDTO) {
        BasicTopic basicTopic = new BasicTopic();
        basicTopic.setId(sysTalkEntDTO.getId());
        basicTopic.setStatus(sysTalkEntDTO.getStatus());
        return sysTalkService.edit(basicTopic);
    }


    /***
     * 删除话题配置
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult delete(@PathVariable String id) {
        return sysTalkService.delete(id);
    }


    @RequestMapping("/setOff")
    public CommonResult setOff(@RequestBody SysTalkEntDTO sysTalkEntDTO) {
        return null;
    }
}
